package com.lrkj.web.phone;

import com.lrkj.common.component.minio.MinioComponent;
import com.lrkj.common.config.RuoYiConfig;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.utils.file.FileUploadUtils;
import com.lrkj.framework.config.ServerConfig;
import com.lrkj.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;

@RestController
public class publicController {

    @Resource
    private ISysDictTypeService dictTypeService;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private MinioComponent minioComponent;

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/phone/dict/type/{dictCode}")
    public AjaxResult dictType(@PathVariable String dictCode) {
        return AjaxResult.success(dictTypeService.selectDictDataByType(dictCode));
    }

    @PostMapping("/phone/common/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            //上传并返回新文件名
            String filePath = minioComponent.uploadByFile(file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", filePath);
            ajax.put("url", filePath);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
